# Makefile for IoT MQTT Client Module

# 编译器设置
CC = gcc
CFLAGS = -Wall -Wextra -std=c99 -O2 -g -pthread
AR = ar
ARFLAGS = rcs

# 目录设置
SRC_DIR = src
INCLUDE_DIR = include
BUILD_DIR = ../../build/mqtt
OBJ_DIR = $(BUILD_DIR)/obj
LIB_DIR = $(BUILD_DIR)/lib
BIN_DIR = $(BUILD_DIR)/bin

# 包含路径
INCLUDES = -I$(INCLUDE_DIR) \
           -I../../common/logs/include \
           -I../../common/config/include \
           -I../../common/json/include

# 源文件和目标文件
SOURCES = $(wildcard $(SRC_DIR)/*.c)
OBJECTS = $(SOURCES:$(SRC_DIR)/%.c=$(OBJ_DIR)/%.o)

# 库文件
STATIC_LIB = $(LIB_DIR)/libmqtt_client.a

# 默认目标
all: directories $(STATIC_LIB)

# 创建目录
directories:
	@mkdir -p $(OBJ_DIR)
	@mkdir -p $(LIB_DIR)
	@mkdir -p $(BIN_DIR)

# 编译静态库
$(STATIC_LIB): $(OBJECTS)
	@echo "创建静态库: $@"
	$(AR) $(ARFLAGS) $@ $^

# 编译目标文件
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c
	@echo "编译: $<"
	$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@

# 清理
clean:
	@echo "清理构建文件..."
	rm -rf $(BUILD_DIR)

# 安装
install: all
	@echo "安装MQTT客户端库..."
	@mkdir -p ../../output/lib
	@mkdir -p ../../output/include/mqtt
	cp $(STATIC_LIB) ../../output/lib/
	cp $(INCLUDE_DIR)/*.h ../../output/include/mqtt/
	@echo "安装完成"

# 帮助信息
help:
	@echo "IoT MQTT客户端模块构建系统"
	@echo "============================"
	@echo "可用目标:"
	@echo "  all      - 构建模块（默认）"
	@echo "  clean    - 清理构建文件"
	@echo "  install  - 安装库文件和头文件"
	@echo "  help     - 显示此帮助信息"

.PHONY: all directories clean install help

